/*
 * @Author: lin_zizhuo 2297304135@qq.com
 * @Date: 2023-11-21 19:09:10
 * @LastEditors: lin_zizhuo 2297304135@qq.com
 * @LastEditTime: 2023-11-21 19:29:44
 * @FilePath: /shared_memory/processb.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "comm.hpp"
#include "log.hpp"
using namespace std;
// processb 负责进行写入操作

int main()
{
    int shmid = shmGeter();
    // cout << shmid << endl;
    // shmat的第二个参数传入的是你想将共享内存的映射放在虚拟地址空间的哪个地方
    // 第三个参数传入控制该进程是只读或者刻度可写
    char *shmem = (char *)shmat(shmid, nullptr, 0);
    if (shmem == (char *)-1)
    {
        getLog().send_message(Fatal, "shmat fail:%s", strerror(errno));
        exit(3);
    }
    getLog().send_message(Info, "shmat success");

    int fd = open(fifo_name, O_WRONLY | O_APPEND);
    if (fd == -1)
    {
        getLog().send_message(Fatal, "open fifo error!\n");
        // perror("open fifo");
        exit(FIFO_OPEN_ERR);
    }

    while (true)
    {
        char c = 'c';
        write(fd, &c, sizeof c);
        cout << "process b say @ ";
        fgets(shmem, shmSize, stdin);
    }
    close(fd);
    int m = shmdt((void *)shmem);
    if (m == -1)
    {
        getLog().send_message(Fatal, "shm unlink fail:%s", strerror(errno));
        exit(5);
    }
    getLog().send_message(Info, "shm unlink success!");
    return 0;
}